package com.lin.fengxiu.api.v1;

import com.lin.fengxiu.bo.PageCounter;
import com.lin.fengxiu.core.LocalUser;
import com.lin.fengxiu.core.interceptors.ScopeLevel;
import com.lin.fengxiu.dto.OrderDTO;
import com.lin.fengxiu.exception.http.NotFoundException;
import com.lin.fengxiu.logic.OrderChecker;
import com.lin.fengxiu.model.Order;
import com.lin.fengxiu.service.OrderService;
import com.lin.fengxiu.util.CommonUtil;
import com.lin.fengxiu.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Optional;

/**
 * @ClassName OrderController
 * @Description TODO 订单API
 * @Author wushaopei
 * @Date 2021/4/2 6:33
 * @Version 1.0
 */
@RestController
@RequestMapping("order")
@Validated
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 延迟支付时间控制
    @Value("${fengxiu.order.pay-time-limit}")
    private Integer payTimeLimit;

    @PostMapping("")
    @ScopeLevel
    public OrderIdVO placeOrder(@RequestBody OrderDTO orderDTO){
        Long uid = LocalUser.getUser().getId();
        // 訂單校驗
        OrderChecker orderChecker = this.orderService.isOK(uid,orderDTO);
        System.out.println("ok");
        // 下單
        Long oid = this.orderService.placeOrder(uid, orderDTO,orderChecker);
        // 返回订单号
        return new OrderIdVO(oid);
    }

    /**
     * @Description TODO 分页查询未支付订单
     * @param start
     * @param count
     * @return
     */
    @ScopeLevel
    @GetMapping("/status/unpaid")
    public PagingDozer getUnpaidSimplifyList(@RequestParam(defaultValue = "0") Integer start,
                                             @RequestParam(defaultValue = "10") Integer count){
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start,count);
        // 获取分页查询对象
        Page<Order> orderPage = this.orderService.getUnpaid(pageCounter.getPage(),pageCounter.getCount());
        // 将分页查询对象 copy 到 VO 模型，避免返回多余的信息
        PagingDozer pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        // 遍历为每个订单set订单有效时长
        pagingDozer.getItems().forEach((o)->((OrderSimplifyVO)o).setPeriod(this.payTimeLimit.longValue()));

        return pagingDozer;
    }

    /**
     * @Description TODO 根据订单状态查询符合状态的订单，支持 ALL、PAID、DELIVERED、FINISHED四种状态订单的查询。
     * @param status
     * @param start
     * @param count
     * @return
     */
    @ScopeLevel
    @GetMapping("/by/status/{status}")
    public PagingDozer getByStatus(@PathVariable int status,
                                    @RequestParam(name = "start",defaultValue = "0") Integer start,
                                    @RequestParam(name = "count",defaultValue = "10") Integer count){
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start,count);
        // 获取分页查询对象
        Page<Order> orderPage = this.orderService.getByStatus(status, pageCounter.getPage(),pageCounter.getCount());
        // 将分页查询对象 copy 到 VO 模型，避免返回多余的信息
        PagingDozer pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        // 遍历为每个订单set订单有效时长
        pagingDozer.getItems().forEach((o)->((OrderSimplifyVO)o).setPeriod(this.payTimeLimit.longValue()));

        return pagingDozer;
    }

    @ScopeLevel
    @GetMapping("/detail/{id}")
    public OrderPureVO getOrderDetail(@PathVariable(name = "id") Long oid){
        Optional<Order> orderOptional = this.orderService.getOrderDetail(oid);
        return orderOptional.map((o) -> new OrderPureVO(o,payTimeLimit))
                .orElseThrow(() -> new NotFoundException(50009));
    }
}
